# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=ocaml-lwt
_pkgname=lwt
pkgver=4.1.0
pkgrel=1
pkgdesc="OCaml promises and concurrent I/O"
url="https://github.com/ocsigen/lwt"
arch="all !x86 !armhf !armv7 !s390x !mips !mips64"  # limited by ocaml aport
license="LGPL-2.1-or-later-WITH-linking-exception BSD-3-Clause"
depends="ocaml-runtime ocaml-result"
depends_dev="libev-dev"
makedepends="$depends_dev
	dune
	ocaml
	ocaml-compiler-libs
	ocaml-cppo-dev
	ocaml-findlib
	ocaml-migrate-parsetree-dev
	ocaml-ppx_tools_versioned-dev
	ocaml-react-dev
	ocaml-result-dev
	opam
	"
options="!check"  # FIXME: tests hang
subpackages="$pkgname-dev ${pkgname}_ppx:_ppx ${pkgname}_react:_react"
source="$pkgname-$pkgver.tar.gz::https://github.com/ocsigen/$_pkgname/archive/$pkgver.tar.gz
	result_lseek_noinline.patch"
builddir="$srcdir/$_pkgname-$pkgver"

build() {
	cd "$builddir"

	ocaml src/util/configure.ml \
		-use-libev true

	jbuilder build @install
}

check() {
	cd "$builddir"

	jbuilder runtest
}

package() {
	local libdir="$pkgdir/usr/lib/ocaml"
	cd "$builddir"

	mkdir -p "$libdir"
	jbuilder install \
		--destdir="$pkgdir"/usr \
		--libdir="$libdir"

	find "$libdir" -name '*.cmxs' -exec chmod 0755 {} \;

	# There's just a readme and changelog.
	rm -Rf "$pkgdir"/usr/doc

	# Remove annotation files and sources.
	find "$libdir"/$_pkgname* \( \
		-name '*.cmt' -o \
		-name '*.cmti' -o \
		-name '*.ml' \) -delete
}

dev() {
	default_dev
	depends="$depends_dev
		$pkgname=$pkgver-r$pkgrel
		${pkgname}_ppx=$pkgver-r$pkgrel
		${pkgname}_react=$pkgver-r$pkgrel"
	provides="${pkgname}_ppx-dev ${pkgname}_react-dev"

	cd "$pkgdir"

	local path; for path in $(find usr/lib/ocaml/$_pkgname*/ \
		-name '*.cmx' \
		-o -name '*.cmxa' \
		-o -name '*.mli')
	do
		mkdir -p "${path%/*}"
		mv "$path" "$subpkgdir"/${path%/*}/
	done
}

_ppx() {
	pkgdesc="$pkgdesc (PPX syntax)"
	depends="$pkgname=$pkgver-r$pkgrel
		ocaml-runtime
		ocaml-migrate-parsetree
		ocaml-ppx_tools_versioned"

	_submv usr/lib/ocaml/lwt_ppx
}

_react() {
	pkgdesc="$pkgdesc (for FRP)"
	depends="$pkgname=$pkgver-r$pkgrel ocaml-runtime ocaml-react"

	_submv usr/lib/ocaml/lwt_react
}

_submv() {
	local path="$1"
	mkdir -p "$subpkgdir"/${path%/*}
	mv "$pkgdir"/$path "$subpkgdir"/${path%/*}/
}

sha512sums="28bc51e75d5e4c74171f17aaba86fdefad70f57f9f2023680044d5bf65250d8531501adc85d0422e644c1e318dcee0a4d53f7841c82d9dca9854e8fbb09987f2  ocaml-lwt-4.1.0.tar.gz
dc3abce70b3ad022066e8023a9b37346df0ecbbc6351186cc24b569189142ad3e7e32f27c33a4378f11ec8c40df5c96f4c37190ca4797bc228ca8943f53cf5bf  result_lseek_noinline.patch"
